<template>
  <!-- 设置 -->
  <view class="pages">
    <view class="content">
      <!-- <view class="item" @click="showMobilePop = true" v-if="userInfo?.phone"> -->
      <view class="item">
        <view class="title">手机号</view>
        <view class="lv-pages-content-bodyer-card-rt">
          <view class="text_style">{{ phoneDesensitization(userInfo?.phone || '') }}</view>
          <!-- <view class="text_style">184****2599</view> -->
        </view>
      </view>
      <view class="item" @click="navigateTo(`/pages-users/common-agreement/index?type=1`)">
        <view class="title">咪婴医生服务协议</view>
        <view class="lv-pages-content-bodyer-card-rt">
          <view class="co-99 f-s-28"></view>
          <u-icon name="arrow-right" color="#aaa" size="16" :bold="true"></u-icon>
        </view>
      </view>
      <view class="item" @click="navigateTo(`/pages-users/common-agreement/index?type=2`)">
        <view class="title">咪婴医生隐私政策</view>
        <view class="lv-pages-content-bodyer-card-rt">
          <view class="co-99 f-s-28"></view>
          <u-icon name="arrow-right" color="#aaa" size="16" :bold="true"></u-icon>
        </view>
      </view>
      <view class="item" @click="requestNotifyPermission">
        <view class="title">当前版本</view>
        <view class="lv-pages-content-bodyer-card-rt">
          <view class="text_style">v{{ editionCode }}</view>
          <!-- <u-icon name="arrow-right" color="#aaa" size="16" :bold="true"></u-icon> -->
        </view>
      </view>
      <view class="item" @click="navigateTo('/pages-users/my-page/cancel-account')">
        <view class="title">注销账号</view>
        <view class="lv-pages-content-bodyer-card-rt">
          <view class="text_style" style="color: #cb5000">账号注销后无法恢复</view>
          <u-icon name="arrow-right" color="#aaa" size="16" :bold="true"></u-icon>
        </view>
      </view>
    </view>

    <view class="log-out" @click="logout" v-if="userStore?.token">退出登录</view>

    <!-- 更新绑定的手机号 -->
    <u-modal
      v-if="userInfo?.phone"
      :show="showMobilePop"
      title="更新绑定的手机号"
      :content="`当前绑定的手机号为${phoneDesensitization(userInfo?.phone)}`"
      showCancelButton
      @confirm="
        () => {
          showMobilePop = false
          navigateTo('/pages-users/my-page/edit-user-mobile')
        }
      "
      @cancel="showMobilePop = false"
      confirmText="更换"
    ></u-modal>

    <!-- 解绑微信 -->
    <u-modal
      :show="showWechatPop"
      title="确定解绑"
      content="解绑微信账号后将无法继续使用它登录该咪婴伴侣账号"
      showCancelButton
      @confirm="unbindWechat"
      @cancel="showWechatPop = false"
      confirmText="确定"
    ></u-modal>

    <h-authpup ref="authpup" type="top" @changeAuth="changeAuth" permissionID="CAMERA"></h-authpup>
  </view>
</template>

<script setup>
import { ref, nextTick } from 'vue'
import { onLoad, onReachBottom, onShow } from '@dcloudio/uni-app'
import {
  showToast,
  navigateTo,
  navigateBack,
  switchTab,
  formatDate,
  phoneDesensitization,
  showLoading,
  hideLoading,
} from '@/utils'
import { useMemberStore, useSystemStore } from '@/stores'
import { setRecommendApi, editUserApi } from '@/services/my/user'
import { getUserInfo } from '@/pages/my/utils'
import { uploadApi } from '@/utils/upLoad'
import { getUserIdentityApi } from '@/api/identity'
import { getEditionCode } from '@/api/patients/index.js'
// 更新绑定的手机号
const showMobilePop = ref(false)
// 解绑微信
const showWechatPop = ref(false)

const authpup = ref(null)

function changeAuth() {
  uni.chooseImage({
    count: 1,
    sizeType: 'compressed',
    success: async (e) => {
      const url = await uploadApi(e.tempFilePaths[0])
      const res = await editUserApi({
        avatar: url,
        is_change_avatar: url ? 1 : 0,
      })
      if (res.message === '编辑成功~') {
        updateUserInfo()
      }
      showToast(res.message)
    },
  })
}

function openAuth() {
  nextTick(() => {
    authpup.value.open()
  })
}
const userInfo = ref({})
onShow(async () => {
  updateUserInfo()
  getEditionCodeFn()
})
const doctorInfo = ref([])
// 获取医生
// async function getDoctor() {
//   const res = (await getMbDoctorList()) || []
//   const res1 = (await termBabyFull()) || []
//   doctorInfo.value.push(...res.data.data, ...res1.data.data)
//   console.log(doctorInfo.value)
// }
//获取版本号
const editionCode = ref('')
async function getEditionCodeFn() {
  const res = await getEditionCode({ is_update: 1 })
  if (res?.code == 200) {
    editionCode.value = res?.data?.version || '1.0.0'
  }
}
/**
 * 更新用户信息
 */
async function updateUserInfo() {
  const res = await getUserInfo()
  userInfo.value = res?.data || {}
}

/**
 * 解绑微信回调
 */
const unbindWechat = () => {
  showWechatPop.value = false
  showToast('解绑成功')
}

const recommend = ref(false)

let userStore = {}
const system = ref()

onLoad(() => {
  try {
    userStore = useMemberStore()
    system.value = useSystemStore()
    recommend.value = userStore?.userInfo?.user?.ext?.is_recommend == 1 ? true : false
    // getDoctor()
  } catch (error) {}
})

/**
 * 个性化推荐
 */
async function changeRecommend(e) {
  showLoading()
  try {
    const res = await setRecommendApi()
    if (res.message === '开启推荐成功') {
      recommend.value = true
    } else if (res.message === '取消推荐成功') {
      recommend.value = false
    }
    showToast(res.message)
    hideLoading()
  } catch (error) {
    hideLoading()
  }
}

function logout() {
  // 询问是否退出登录
  uni.showModal({
    title: '提示',
    content: '是否退出登录',
    success: function (res) {
      if (res.confirm) {
        userStore?.clearUserInfo()
        userStore?.setislogin(0)
        userStore?.clearToken()
        system.value.setCasualUserInfo('')
        system.value.setNewInfo('')
        if (doctorInfo.value.length) {
          doctorInfo.value.forEach((item) => {
            uni.setStorageSync(item?.doctor_im_id, [])
          })
        }
        getUserIdentity()
      }
    },
  })
}
/**
 * 获取喜好选择
 */
const equipment = ref()
function getUserIdentity() {
  const userStore = useMemberStore()
  const system = useSystemStore()
  if (userStore?.token) return
  uni.getSystemInfo({
    success: async (e) => {
      equipment.value = e.deviceId
      try {
        const res = await getUserIdentityApi({
          equipment: e.deviceId,
        })
        system.setCasualUserInfo(res.data)
        userStore.setCasualUser(res.data)
        showToast('退出成功')
        setTimeout(() => {
          switchTab('/pages/my/index')
        })
      } catch (error) {
        console.log(3, error)
      }
    },
  })
}
</script>
<style>
page {
  background-color: #f6f6f6;
}
</style>
<style scoped lang="scss">
.pages {
  width: 750rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-bottom: 80rpx;
  padding: 25rpx 24rpx;
  .content {
    width: 100%;
    padding: 0 24rpx;
    border-radius: 16rpx;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 20rpx;
    background-color: #fff;
    :last-child {
      border: none !important;
    }
    .item {
      width: 100%;
      height: 100rpx;
      border-bottom: 1rpx solid #e6e6e6;
      display: flex;
      align-items: center;
      justify-content: space-between;
      .title {
        font-weight: 500;
        font-size: 28rpx;
        color: #5c5c5c;
      }
      .lv-pages-content-bodyer-card-rt {
        display: flex;
        justify-content: flex-end;
        .text_style {
          font-size: 28rpx;
          color: #202020;
        }
        .head-bg {
          width: 90rpx;
          height: 60rpx;
          background-color: #f6f6f6;
        }
        .lv-pages-content-bodyer-card-picker-pl {
          color: #868686;
          font-size: 24rpx;
        }
        ::v-deep .placeholder-class {
          font-size: 24rpx;
        }
        .text-input {
          text-align: right;
        }
        ::v-deep .u-icon {
          // margin-left: 8rpx;
        }
      }
    }
  }
  .log-out {
    width: 702rpx;
    height: 89rpx;
    background: #ffffff;
    border-radius: 16rpx;
    opacity: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 32rpx;
    font-weight: 500;
    color: #333333;
  }
}
</style>
